<template>
  <div>
    <van-nav-bar title="登陆" left-arrow @click-left="onClickLeft" />
    <van-form colon label-align="right" class="login-box" @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="账号"
        placeholder="账号"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 20px 50px">
        <van-button
          round
          block
          type="info"
          color="linear-gradient(to right, #ff6034, #ee0a24)"
          native-type="submit"
          >立即登录</van-button
        >
      </div>
    </van-form>
    <div class="register-link" @click="changeRegister">
      没有账号？立即注册！
    </div>
  </div>
</template>

<script>
import { NavBar, Field, Form, Button, Notify,Toast } from "vant";

import { Login } from "@/network/index.js";

export default {
  name: "Login",
  data() {
    return {
      username: "",
      password: "",
    };
  },
  components: {
    [NavBar.name]: NavBar,
    [Field.name]: Field,
    [Button.name]: Button,
    [Form.name]: Form,
    [Toast.name]: Toast,
    [Notify.Component.name]: Notify.Component,
  },
  methods: {
    onClickLeft() {
      //返回首页
      this.$router.replace("/Home");
    },
    async onSubmit() {
      //登录提交
      let info = {
        //保存表单信息
        username: this.username,
        password: this.password,
      };
      const {
        data: { data },
      } = await Login(info); //进行登录请求
      if (data) {
        const endTime = +new Date() + 7199990
      
        Toast.loading({
          message: "正在登录",
          forbidClick: true,
          loadingType: "spinner",
        });
        this.$store.commit('setEndTime',endTime) //保存注销时间
        this.$store.commit("setUserInfo", data); //把用户信息保存进 vuex
        this.$store.commit("savePwd", this.password);
        
        setTimeout(() => {
          Toast.success("登录成功");
          this.$router.replace("/Profile"); //回到个人中心路由`
        }, 1000);
      } else {
        Notify({ type: "danger", message: "账号密码有误" });
      }
    },
    changeRegister() {
      this.$router.replace("/Register"); //跳转至注册
    },
  },
  created() {},
  mounted() {},
};
</script>

<style lang="less" scoped>
.van-field--label-right {
  margin: 20px auto;
}
.login-box {
  margin-top: 100px;
  // margin: 30px auto;
}
.register-link {
  font-size: 14px;
  color: gray;
  margin: 0 50px;
  text-align: right;
}
</style>

